package com.ydd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ydd.dto.CatePageDTO;
import com.ydd.entity.YddCate;
import com.ydd.mapper.CateMapper;
import com.ydd.result.PageResult;
import com.ydd.service.CateService;
import com.ydd.vo.CateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CateServiceImpl implements CateService {
    @Autowired
    private CateMapper cateMapper;

    /**
     * 分类详细
     *
     * @param id
     * @return
     */
    @Override
    public CateVO rowInfo(Long id) {
        //数据库对象装
        //调用Catemapper查询数据
        CateVO cateVO = cateMapper.rowInfo(id);
        //返回VO对象
        return cateVO;
    }

    /**
     * 分页查询
     *
     * @return
     */
    @Override
    public PageResult pageList(CatePageDTO catePageDTO) {
        PageHelper.startPage(catePageDTO.getPage(),catePageDTO.getLimit());
        Page<YddCate> voPage = cateMapper.pageList(catePageDTO);
        if(catePageDTO.getLimit()==0){
        }
            Long totalPages = voPage.getTotal()/catePageDTO.getLimit();
            int intTotalPages = totalPages.intValue();
        return new PageResult(voPage.getTotal(),catePageDTO.getPage(),
                voPage.getResult(),catePageDTO.getLimit(),intTotalPages+1);
    }

    /**
     * 根据分类id删除
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        cateMapper.deleteById(id);
    }

    /**
     * 修改分类信息
     *
     * @param yddCate
     */
    @Override
    public void update(YddCate yddCate) {
        cateMapper.updateById(yddCate);
    }

    /**
     * 增加分类信息
     *
     * @param yddCate
     */
    @Override
    public void add(YddCate yddCate) {
        yddCate.setDefaultData("1");
        cateMapper.add(yddCate);
    }

    /**
     * 查询分类列表
     *
     * @return
     */
    @Override
    public YddCate[] cateAll() {
        YddCate[] yddCateList = cateMapper.all();
        return yddCateList;
    }
}
